require 'utils.rb'

$drop_property = PropertyTemplate.find(31)

def find_monster_type(type_name)
  begin
    return find_monster_type_by_name(type_name).id
  rescue Exception => e
    puts e
    return 0
  end
end

def find_map(map_name)
  begin
    return find_map_by_name(map_name).id
  rescue Exception => e
    return 0
  end
end

def find_dungeon(dungeon_name)
  begin
    return find_dungeon_by_name(dungeon_name).id
  rescue Exception => e
    return 0
  end
end

def find_item(item_name)
  #~ begin
    return find_item_by_name(item_name)
  #~ rescue Exception => e
    #~ puts e
    #~ return nil
  #~ end
end

def find_monster(monster_name)
  begin
    return find_monster_by_name(monster_name)
  rescue Exception => e
    return nil
  end
end

def check_property(item, monster)
  properties = item.properties
  return nil if properties.nil?
  return nil if properties.empty?
  properties.split(",").each do |property_id|
    property = ItemProperty.find(property_id)
    next unless property.template_id == $drop_property.id
    return property if property.reserved1 == monster.id
  end
  return nil
end

def add_property_to_item(property, item)
  property_id = property.id.to_s
  if item.properties.nil?
    item.properties = property_id
  elsif item.properties.empty?
    item.properties = property_id
  else
    old = item.properties
    item.properties = "#{old},#{property_id}"
  end
  item.save
end

def create_item_property(item, monster)
  begin
    property = check_property(item, monster)
    return unless property.nil?

    property = ItemProperty.new
    property.template_id = $drop_property.id
    property.item_id = item.id
    property.reserved1 = monster.id
    property.reserved4 = monster.japaness_name
    desc = $drop_property.description
    desc.gsub!(/\$\(r4\)/, monster.japaness_name)
    property.description = desc
    property.save
  
    add_property_to_item(property, item)
  rescue Exception => e
    raise "#{e} with item:#{item.id}-#{item.japaness_name}"
  end
end

File.open('templates/monster.txt', 'r') do |file|
  monster_id = 1
  while line = file.gets
    line.chop!
    strs = line.split("\t")
    #~ monster = find_monster(strs[0])
    begin
      monster = Monster.find(monster_id)
    rescue
      monster = Monster.new
      monster.id = monster_id
    end
    #~ monster_id = monster.id
    monster.japaness_name = strs[0]
    monster.monster_type = find_monster_type(strs[1])
    maps = []
    map_id = find_map(strs[2])
    maps << map_id unless map_id == 0
    map_id = find_map(strs[3])
    maps << map_id unless map_id == 0
    monster.maps = maps.join(",")
    drops = []
    4.upto strs.size() -1 do |i|
      begin
        item = find_item(strs[i])
        drops << item.id
        create_item_property(item, monster)
      rescue Exception => e
        puts "#{e} - monster: #{monster_id}-#{monster.japaness_name}"
      end
    end
    monster.drops = drops.join(",")
    monster.save
    monster_id = monster_id + 1
  end
end
